//
//  HomeViewController.m
//  sharedBooks
//
//  Created by XunFengKJ on 2016/11/9.
//  Copyright © 2016年 XunFengKJ. All rights reserved.
//

#import "XFTarbarViewController.h"
#import "HTHomeViewController.h"
#import "HSOrderViewController.h"
#import "BaseNavigationController.h"
#import "LoginViewController.h"

@interface XFTarbarViewController ()<UITabBarControllerDelegate>

@end

@implementation XFTarbarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupAllChildViewControllers];
    lNOTIF_ADD(@"clickHomeMessageBtnNoti", clickHomeMessageBtn);
}

-(void)clickHomeMessageBtn{ //点击首页的消息按钮，跳转订单
    self.selectedViewController = [self.viewControllers objectAtIndex:1];//默认选中1
}

- (void)setupAllChildViewControllers{
    HTHomeViewController *HomeVC = [[HTHomeViewController alloc] init];
    [self setupChildViewController:HomeVC title:@"首页" imageName:@"tab_home" selectedImageName:@"tab_home_pressed"];
    
    HSOrderViewController*orderVC = [[HSOrderViewController alloc] init];
    [self setupChildViewController:orderVC title:@"订单" imageName:@"tab_order" selectedImageName:@"tab_order_pressed"];
}

- (void)setupChildViewController:(UIViewController *)childVc title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName{
    
    childVc.title = title;
    self.tabBar.tintColor = LCOLOROFNAV;
    self.delegate = self;
    [[UINavigationBar appearance] setTranslucent:NO];
    childVc.tabBarItem.image = [UIImage imageNamed:imageName];
    childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    BaseNavigationController * Nav = [[BaseNavigationController alloc] initWithRootViewController:childVc];
    [self addChildViewController:Nav];
}

//登录
- (void)login{
    NSString *userName = [XFKeychainTool readUserName];
    NSString *passW = [XFKeychainTool readPassword].md5TolowercaseString;
    WEAKSELF
    [XFPostRequest postRequestLoginUsername:userName Password:passW success:^(id responseObject) {
        LGFLog(@"登录 - %@",responseObject);
        //登录成功
        if ([responseObject[@"status"] intValue] == 200) {
            NSArray *secondUser = [[responseObject safeJsonObjectForKey:@"content"] safeJsonObjectForKey:@"roles"];
            if (secondUser.count) {
                NSDictionary *dict = secondUser[0];
                [BookShareUserInforDefault saveiscanOpenSecondUser:[dict safeJsonObjectForKey:@"name"]];
            }
            NSString *userID = [[responseObject safeJsonObjectForKey:@"content"] safeJsonObjectForKey:@"id"];
            NSArray *dataArr = [[responseObject safeJsonObjectForKey:@"content"] safeJsonObjectForKey:@"shops"];
            if (dataArr.count>0) {
                NSDictionary *data = dataArr[0];
                [weakSelf saveUserDataWithDict:data];
            }
            //极光
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
               NSString *alias =  [NSString stringWithFormat:@"%@",userID];
            [JPUSHService setTags:nil alias:alias fetchCompletionHandle:^(int iResCode, NSSet *iTags, NSString *iAlias) {
            LGFLog(@"//极光 iResCode-%zd iTags=%@ alias = %@",iResCode,iTags,alias);
                                }];
             });
        }else{
            [SOAutoHideMessageView showMessage:[responseObject safeJsonObjectForKey:@"msg"] inView:self.view];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.75 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                BaseNavigationController * Nav = [[BaseNavigationController alloc] initWithRootViewController:[LoginViewController new]];
                kAppWindow.rootViewController = Nav;
            });
        }
    } failure:^(NSError *error) {
        LGFLog(@"error - %@",error);
        [MBProgressHUD hideHUDForView:weakSelf.view];
        [SOAutoHideMessageView showMessage:LNETWORKERROR inView:weakSelf.view];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.75 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            BaseNavigationController * Nav = [[BaseNavigationController alloc] initWithRootViewController:[LoginViewController new]];
            kAppWindow.rootViewController = Nav;
        });
    }];
}

-(void)saveUserDataWithDict:(NSDictionary*)contentDict{
    [BookShareUserInforDefault saveLoginStatus:YES];
    [BookShareUserInforDefault saveUserAvatarUrl:[contentDict safeJsonObjectForKey:@"imanges"]];
    [BookShareUserInforDefault saveUserAddress:[contentDict safeJsonObjectForKey:@"address"]];
    [BookShareUserInforDefault saveUserBegin:[contentDict safeJsonObjectForKey:@"begin"]];
    [BookShareUserInforDefault saveUserEnd:[contentDict safeJsonObjectForKey:@"end"]];
    [BookShareUserInforDefault saveUserUid:[contentDict safeJsonObjectForKey:@"id"]];
    [BookShareUserInforDefault saveUserIntro:[contentDict safeJsonObjectForKey:@"intro"]];
    [BookShareUserInforDefault saveUserNicknames:[contentDict safeJsonObjectForKey:@"name"]];
    [BookShareUserInforDefault saveUserPhone:[contentDict safeJsonObjectForKey:@"phoneNumber"]];
}

@end
